package com.task.core.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.task.common.redis.RedisKeyConstants;
import org.springframework.stereotype.Service;

import com.task.common.annotation.DataScope;
import com.task.common.core.redis.RedisCache;
import com.task.common.domain.info.KeyAndValue;
import com.task.common.exception.ServiceException;
import com.task.common.info.DeviceGroupInfo;
import com.task.common.info.FlowFilterInfo;
import com.task.common.utils.uuid.UUID;
import com.task.core.dto.request.DeviceGroupRequestDTO;
import com.task.core.mapper.DeviceGroupMapper;
import com.task.core.service.DeviceGroupService;

/**
 * @author 迪迦.
 * @date 2024/11/5 16:28
 */
@Service("DeviceGroupService")
public class DeviceGroupServiceImpl implements DeviceGroupService {

  @Resource
  private DeviceGroupMapper deviceGroupMapper;

  @Resource
  private RedisCache redisService;

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<DeviceGroupInfo> getDeviceGroupByPage(DeviceGroupRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return deviceGroupMapper.getDeviceGroupByPage(request);
  }

  @Override
  @DataScope(deptAlias = "dept", userAlias = "su")
  public List<KeyAndValue> getDeviceGroupForOption(DeviceGroupRequestDTO request) {
    //request.setOperationBy(SecurityUtils.checkAdmin());
    return deviceGroupMapper.getDeviceGroupForOption(request);
  }

  @Override
  public void addDeviceGroup(DeviceGroupRequestDTO request) {
    request.setCode(UUID.randomUUID().toString().replace("-", ""));
    deviceGroupMapper.addDeviceGroup(request);
    redisService.setCacheObject(RedisKeyConstants.DEVICE_GROUP_INFO.formatKey(request.getCode()), new DeviceGroupInfo(request.getCode(), request.getDeviceGroupName(),
        request.getDescription()));
  }

  @Override
  public void modifyDeviceGroup(DeviceGroupRequestDTO request) {
    Integer integer = deviceGroupMapper.modifyDeviceGroup(request);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    redisService.setCacheObject(RedisKeyConstants.DEVICE_GROUP_INFO.formatKey(request.getCode()), new DeviceGroupInfo(request.getCode(), request.getDeviceGroupName(),
        request.getDescription()));
  }

  @Override
  public void deleteDeviceGroup(String deviceGroupCode, Long userId) {
    Integer integer = deviceGroupMapper.deleteDeviceGroup(deviceGroupCode, userId);
    if(integer == 0) {
      throw new ServiceException("code不存在");
    }
    //删除关联数据
    //删除任务和设备分组关联
    deviceGroupMapper.deleteMissionDeviceGroupRelation(deviceGroupCode);
    redisService.deleteObject(RedisKeyConstants.DEVICE_GROUP_INFO.formatKey(deviceGroupCode));
  }

  @Override
  public DeviceGroupInfo getDeviceGroupByCode(String deviceCode) {
    return deviceGroupMapper.getDeviceGroupByCode(deviceCode);
  }

  @Override
  public List<DeviceGroupInfo> getDeviceGroupByMissionCode(String missionCode) {
    return deviceGroupMapper.getDeviceGroupByMissionCode(missionCode);
  }

  @Override
  @DataScope(deptAlias = "dept",userAlias = "su")
  public DeviceGroupInfo getDeviceGroupDetailByCode(String deviceGroupCode) {
    DeviceGroupRequestDTO deviceGroupRequestDTO = new DeviceGroupRequestDTO();
    deviceGroupRequestDTO.setCode(deviceGroupCode);
    return deviceGroupMapper.getDeviceGroupDetailByCode(deviceGroupRequestDTO);
  }
}
